<?php


namespace backend\modules\tool\controllers;


use backend\modules\tool\DataSource\Config\Config;
use backend\modules\tool\DataSource\Log\Log;
use backend\modules\tool\DataSource\Log\PidRecord;
use backend\modules\tool\DataSource\Task\BaseTask;
use backend\modules\tool\DataSource\Task\command\CommandFaced;
use backend\modules\tool\DataSource\Task\command\WindowsCommad;
use backend\modules\tool\helpers\Context;
use backend\modules\tool\Job\SqlNode;
use backend\modules\tool\models\DataSourceTask;
use backend\modules\tool\models\Node;

class IndexController extends BaseTask
{
    protected $task;
    protected $class_path;
    protected $fun;
    /**
     * @var Log $log
     */
    protected $log;
    protected $is_link=true;
    /**
     * @var PidRecord
     */
    protected $pid_record;
    public function recordMyPid()
    {
        // TODO: Implement recordMyPid() method.
    }
    public function beforeRun()
    {
        try {
            $this->task = DataSourceTask::find()->where(["id" => $this->task_id])->one();
            $this->class_path = $this->task->class_path;
            $fun = Config::run($this->class_path);
            $this->fun = $fun;
            if($this->class_path==SqlNode::class) {
                $this->pid_record = PidRecord::SingleTon($this->task->task_id);
                $this->log = Log::SingleTon($this->task->task_id);
                $this->log->write("任务开始运行" . date("Y-m-d H:i:s"));

            }else{
                $class_path=str_replace("\\","_",$this->class_path);
                $this->pid_record = PidRecord::SingleTon($this->task->task_id.$class_path);
                $this->log = Log::SingleTon($this->task->task_id.$class_path);
                $this->log->write("任务开始运行" . date("Y-m-d H:i:s"));
            }
            Context::SingleTon()->setLog($this->log);
            Context::SingleTon()->set("hanndle_num", 0);
            Context::SingleTon()->set("memory", memory_get_usage());
            Context::SingleTon()->set("start_at", microtime(true));
        }
        catch (\Throwable $exception){
            $this->log->write("任务执行发生异常");
            $this->log->write($exception);
            $this->afterRun();
            exit();
        }
    }
    public function actionIndex(){
        try {
            if($this->pid_record->IsRun()){
                $this->is_link=false;
                throw new \Exception("任务运行发生冲突，同一个任务在同一时间段只允许运行一次,本次任务任务运行已自动调度到下一个时间节点运行");
            }
            $this->pid_record->recordMyPid();;
            call_user_func($this->fun, $this->class_path);
        }
        catch (\Throwable $throwable){
            $this->log->write("任务运行发生异常".PHP_EOL.$throwable.date("Y-m-d H:i:s"));
            if(CommandFaced::getDirver() instanceof WindowsCommad) {
                echo $throwable;
                sleep(100);
            }
        }
        finally{
            echo $this->getRunTime();
        }
    }
    public function afterRun()
    {
        if($this->is_link) {
            $this->pid_record->finish();
        }
        try {
            Context::SingleTon()->getLog()->write("程序运行时间=====>" . round(microtime(true) - Context::SingleTon()->get("start_at"))."S");
            $this->log->write("程序消耗内存======>".round(memory_get_usage()/1024/1024, 2).'MB');
            $this->log->write("处理数量======>".Context::SingleTon()->get("hanndle_num"));
            $this->log->write("任务运行完成" . date("Y-m-d H:i:s"));
            $this->log->write("=======================>");
            echo $this->getRunTime();
        }
        catch (\Throwable $exception){
            $this->log->write("任务运行发生异常".PHP_EOL.$exception.date("Y-m-d H:i:s"));
            $this->log->write("=======================>");
        }
    }
}